package com.gitee.aurora.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gitee.aurora.dao.DataCheckDao;
import com.gitee.aurora.entity.TabAjAqBaqsyqkdjb;
import com.gitee.aurora.service.DataCheckService;
import com.gitee.aurora.utils.ResultVO;
import com.gitee.aurora.utils.ResultVOUtils;
import com.gitee.aurora.vo.DataCheckVo;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * author: jiangfuxiao
 * Date:2022/9/2 11:34
 */
@Service("dataCheckServiceImpl")
public class DataCheckServiceImpl implements DataCheckService {

    @Resource(name = "dataCheckDaoImpl")
    private DataCheckDao dataCheckDao;

    @Resource
    private RestTemplate restTemplate;


    @Override
    public ResultVO checkBaq(DataCheckVo checkVo) {
        List<TabAjAqBaqsyqkdjb> listBaq = new ArrayList<>();
        List<TabAjAqBaqsyqkdjb> listSjzx = new ArrayList<>();
        if (checkVo == null){
            return ResultVOUtils.error("参数错误，接口调用失败");
        }
        String code = dataCheckDao.queryCodeList(checkVo.getCode());
        String baqUrl ="http://"+checkVo.getBaqIp()+":9050/baqCheck/queryTableData?sql="+checkVo.getSql()+"&tableName="+checkVo.getTableName()+"&startTime="+checkVo.getStartTime()+"&endTime="+checkVo.getEndTime();
        String sjzxUrl ="http://"+checkVo.getSjzxIp()+":9053/checkSjzx/queryTableData?tableName="+checkVo.getTableName()+"&startTime="+checkVo.getStartTime()+"&endTime="+checkVo.getEndTime()+"&code="+code;
        try {
            HttpHeaders headers = new HttpHeaders();
            ResponseEntity<ResultVO> responseBaq = restTemplate.exchange(
                baqUrl,
                HttpMethod.GET,
                new HttpEntity<String>(headers),
                ResultVO.class);
            listBaq = JSONObject.parseArray(responseBaq.getBody().getData().toString(), TabAjAqBaqsyqkdjb.class);
            ResponseEntity<ResultVO> responseSjzx = restTemplate.exchange(
                sjzxUrl,
                HttpMethod.GET,
                new HttpEntity<String>(headers),
                ResultVO.class);
            listSjzx = JSONObject.parseArray(responseSjzx.getBody().getData().toString(), TabAjAqBaqsyqkdjb.class);
        }catch (Exception e){
            e.printStackTrace();
            return  ResultVOUtils.error("调用数据中心或办案区接口失败，请检查ip是否正确");
        }

        if (CollectionUtils.isEmpty(listBaq) && CollectionUtils.isEmpty(listSjzx)){
            return  ResultVOUtils.error("调用数据中心或办案区接口未查询到数据集合");
        }
        if (listBaq.size()>listSjzx.size()){
            return ResultVOUtils.success(listBaq);
        }

        return ResultVOUtils.success(true);
    }

    @Override
    public ResultVO insertData(List list,DataCheckVo checkVo) {
        String sjzxUrl ="http://"+checkVo.getSjzxIp()+":9053/checkSjzx/insertData?";
        HttpEntity<List> httpEntity = new HttpEntity<>(list);
        ResponseEntity<ResultVO> responseSjzx = restTemplate.exchange(
                sjzxUrl,
                HttpMethod.POST,
                httpEntity,
                ResultVO.class);
        ResultVO body = responseSjzx.getBody();
        return body;
    }

}
